import {
    RECEIVE_ADDRESS,
    RECEIVE_CATEGORYS,
    RECEIVE_SHOPS,
    RECEIVE_USER_INFO,
    RESET_USER_INFO,
    RECEIVE_GOODS,
    RECEIVE_RATINGS,
    RECEIVE_INFO,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_CART,
    RECEIVE_SEARCH_SHOPS
} from './mutation-types'

import {
    reqAddress,
    reqCategory,
    reqShops,
    reqUserInfo,
    reqLogout,
    reqShopGoods,
    reqShopRatings,
    reqShopInfo,
    reqSearchGoods
} from '../api/index.js'

export default {
    // 接收地址信息
    async getAddress({ commit, state }) {
        let { latitude, longitude } = state
        // let latitude = state.latitude
        // let longitude = state.longitude
        let params = latitude + ',' + longitude
        let result = await reqAddress(params)
        // commit('mutations里面的方法名字','传递的对象')
        commit(RECEIVE_ADDRESS, { address: result.data })
    },
    // 分类数组 
    async getCategorys({ commit }) {
        let result = await reqCategory()
        commit(RECEIVE_CATEGORYS, { categoryx: result.data })
    },
    // 商家数组
    async getShops({ commit, state }) {
        let { latitude, longitude } = state
        let result = await reqShops({ latitude, longitude })
        commit(RECEIVE_SHOPS, { shops: result.data })
    },
    // 获取个人信息
    recordUser({ commit }, user) {
        commit(RECEIVE_USER_INFO, { userInfo: user })
    },
    // 异步获取用户会话信息
    async getUserInfo({ commit }) {
        let result = await reqUserInfo()
        if (result.code == 0) {
            let user = result.data
            commit(RECEIVE_USER_INFO, { userInfo: user })
        }
    },
    // 异步退出登录 
    async getLogOut({ commit }) {
        let result = await reqLogout()
        if (result.code == 0) {
            commit(RESET_USER_INFO)
        }
    },
    // 异步获取商家商品信息 
    async getShopGoods({ commit },callback) {
        let result = await reqShopGoods()
        if (result.code == 0) {
            let goods = result.data
            commit(RECEIVE_GOODS, { goods })
            callback&callback()
        }
    },
    //异步获取商家商品评价
    async getShopRatings({ commit },callback) {
        let result = await reqShopRatings()
        if (result.code == 0) {
            let ratings = result.data
            commit(RECEIVE_RATINGS, { ratings })
            callback&callback()
        }
    },
    // 异步获取商家个人信息
    async getShopInfo({ commit }) {
        let result = await reqShopInfo()
        if (result.code == 0) {
            let info = result.data
            commit(RECEIVE_INFO, { info })
        }
    },
    // 更换购物车数量方法
    updateFoodCount({commit},{isflag,food}){
        if(isflag){ //添加
            commit(INCREMENT_FOOD_COUNT,{food})
        }else{ //减少
            commit(DECREMENT_FOOD_COUNT,{food})
        }
    },
    // 清空购物车
    clearCart({commit}){
        commit(CLEAR_CART)
    },
    // 异步搜索商家列表
    async searchShops({commit,state},keyword){
        const {latitude,longitude}=state
        let params = latitude + ',' + longitude
        const result = await reqSearchGoods(params,keyword)
        commit(RECEIVE_SEARCH_SHOPS,{searchShops:result.data})
    }
}